﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using V5_Common;
using System.Web.Compilation;
using System.IO;
using System.Web.WebPages;

namespace V5_EShop._Class.Data {
    public class V5HtmlHelper {
        private static readonly V5HtmlHelper m_Instance = new V5HtmlHelper();
        /// <summary>
        /// 
        /// </summary>
        public static V5HtmlHelper Instance {
            get { return m_Instance; }
        }
        /// <summary>
        /// 在模板目录下获取包含文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string Include(string filePath) {
            try {
                return V5.Html.Include("~/Template/" + V5.Config.TemplateDir + "/" + filePath);
            }
            catch (Exception ex) {
                return ex.Message;
            }
        }
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public HtmlString Partial(string filePath) {
            string templatePath = "/Template/" + V5.Config.TemplateDir + "/" + filePath + ".cshtml";
            var path = System.IO.Path.Combine(HttpContext.Current.Request.ApplicationPath, templatePath);
            var type = BuildManager.GetCompiledType(path);
            var script = Activator.CreateInstance(type) as System.Web.WebPages.WebPage;
            script.VirtualPath = "~" + templatePath;
            //
            var stringWriter = new StringWriter();
            script.ExecutePageHierarchy(new WebPageContext(
                    new HttpContextWrapper(HttpContext.Current), script, null
                    ), stringWriter);
            return new HtmlString(stringWriter.ToString());
        }
    }
}